package com.zyh.scs.admin.base.open;

import com.zyh.scs.admin.api.constant.open.CodeSet;
import lombok.Data;
import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * @param <T>
 * @author hwh
 */
@Data
@ToString
public class LaoSheResponse<T> implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private static final Logger log = LoggerFactory.getLogger(LaoSheResponse.class);

    protected Integer errCode;

    protected String errMsg;

    protected String traceId;

//	protected Long Timestamp;
//
//	protected Long centerTime;

    protected T data;

    public LaoSheResponse() {
        this.errCode = CodeSet.SYSTEM_EXCEPTION.getCode();
        this.errMsg = "处理失败";
        this.traceId = "";
    }

    public LaoSheResponse(Integer code, String msg, String traceId) {
        this.errCode = code;
        this.errMsg = msg;
        this.traceId = traceId;
    }

    public static <T> LaoSheResponse<T> success(String msg, String traceId) {
        LaoSheResponse<T> result = new LaoSheResponse<>();
        result.setErrCode(CodeSet.SUCCESS.getCode());
        result.setErrMsg(msg);
        result.setTraceId(traceId);
        return result;
    }

    public static <T> LaoSheResponse<T> successData(T object, String traceId) {
        LaoSheResponse<T> result = new LaoSheResponse<>();
        result.setErrCode(CodeSet.SUCCESS.getCode());
        result.setData(object);
        result.setErrMsg("处理成功");
        result.setTraceId(traceId);
        return result;
    }

    public static <T> LaoSheResponse<T> successRollBackRecharge(String traceId) {
        LaoSheResponse<T> result = new LaoSheResponse<>();
        result.setErrCode(CodeSet.SUCCESS.getCode());
        result.setErrMsg("处理成功");
        result.setTraceId(traceId);
//		result.setTimestamp(System.currentTimeMillis());
        return result;
    }

    public static <T> LaoSheResponse<T> successCheck(T object, String traceId) {
        LaoSheResponse<T> result = new LaoSheResponse<>();
        result.setErrCode(CodeSet.SUCCESS.getCode());
        result.setErrMsg("处理成功");
        result.setData(object);
        result.setTraceId(traceId);
//		result.setCenterTime(System.currentTimeMillis());
        return result;
    }

    public static <T> LaoSheResponse<T> error(CodeSet codeSet, String traceId) {
        LaoSheResponse<T> result = new LaoSheResponse<>();
        result.setErrCode(codeSet.getCode());
        result.setErrMsg(codeSet.getCodeName());
        result.setTraceId(traceId);
        log.error("第三方接口请求异常, traceId: {}, errMsg: {}", traceId, codeSet.getCodeName());
        return result;
    }

    public static <T> LaoSheResponse<T> error(Integer errCode, String errMsg, String traceId) {
        LaoSheResponse<T> result = new LaoSheResponse<>();
        result.setErrCode(errCode);
        result.setErrMsg(errMsg);
        result.setTraceId(traceId);
        log.error("第三方接口请求异常, traceId: {},  errMsg: {}", traceId, errMsg);
        return result;
    }

    public static <T> LaoSheResponse<T> error() {
        LaoSheResponse<T> result = new LaoSheResponse<>();
        result.setErrCode(CodeSet.SYSTEM_EXCEPTION.getCode());
        result.setErrMsg("系统处理失败");
        result.setTraceId("");
        return result;
    }

    public void setCodeSet(CodeSet codeSet) {
        this.setErrCode(codeSet.getCode());
        this.setErrMsg(codeSet.getCodeName());
        this.setTraceId("");
    }

}